import request from './request'

/**
 * 成绩组成相关接口
 */
export const scoreComponentApi = {
  /**
   * 获取指定教学任务的所有成绩组成
   * @param {number} teachingMissionId 教学任务ID
   */
  getScoreComponentsByTeachingMission(teachingMissionId) {
    return request({
      url: `/api/score-components/by-teaching-mission/${teachingMissionId}`,
      method: 'get'
    })
  },

  /**
   * 根据ID获取成绩组成
   * @param {number} id 成绩组成ID
   */
  getScoreComponentById(id) {
    return request({
      url: `/api/score-components/${id}`,
      method: 'get'
    })
  },

  /**
   * 创建成绩组成
   * @param {Object} scoreComponent 成绩组成信息
   */
  createScoreComponent(scoreComponent) {
    return request({
      url: '/api/score-components',
      method: 'post',
      data: scoreComponent
    })
  },

  /**
   * 更新成绩组成
   * @param {number} id 成绩组成ID
   * @param {Object} scoreComponent 成绩组成信息
   */
  updateScoreComponent(id, scoreComponent) {
    return request({
      url: `/api/score-components/${id}`,
      method: 'put',
      data: scoreComponent
    })
  },

  /**
   * 删除成绩组成
   * @param {number} id 成绩组成ID
   */
  deleteScoreComponent(id) {
    return request({
      url: `/api/score-components/${id}`,
      method: 'delete'
    })
  }
}

/**
 * 成绩相关接口
 */
export const scoreApi = {
  /**
   * 根据学期ID和课程ID获取最终成绩
   * @param {number} termId 学期ID
   * @param {number} courseId 课程ID
   */
  getFinalScoresByTermAndCourse(termId, courseId) {
    return request({
      url: `/api/scores/final/by-term/${termId}/course/${courseId}`,
      method: 'get'
    }).catch(error => {
      return []; // 失败时返回空数组，确保前端能够继续处理
    })
  },
  
  /**
   * 根据学期ID和课程ID获取部分成绩
   * @param {number} termId 学期ID
   * @param {number} courseId 课程ID
   */
  getPartScoresByTermAndCourse(termId, courseId) {
    return request({
      url: `/api/scores/part/by-term/${termId}/course/${courseId}`,
      method: 'get'
    }).catch(error => {
      return []; // 失败时返回空数组，确保前端能够继续处理
    })
  },

  /**
   * 根据教学任务ID获取最终成绩
   * @param {number} teachingMissionId 教学任务ID
   */
  getFinalScoresByTeachingMission(teachingMissionId) {
    return request({
      url: `/api/scores/final/by-teaching-mission/${teachingMissionId}`,
      method: 'get'
    })
  },
  
  /**
   * 根据教学任务ID和学生ID获取最终成绩
   * @param {number} teachingMissionId 教学任务ID
   * @param {number} studentId 学生ID
   */
  getFinalScoreByTeachingMissionAndStudent(teachingMissionId, studentId) {
    return request({
      url: `/api/scores/final/by-teaching-mission/${teachingMissionId}/student/${studentId}`,
      method: 'get'
    })
  },

  /**
   * 根据教学任务ID获取部分成绩
   * @param {number} teachingMissionId 教学任务ID
   */
  getPartScoresByTeachingMission(teachingMissionId) {
    return request({
      url: `/api/scores/part/by-teaching-mission/${teachingMissionId}`,
      method: 'get'
    })
  },
  
  /**
   * 根据教学任务ID和学生ID获取部分成绩
   * @param {number} teachingMissionId 教学任务ID
   * @param {number} studentId 学生ID
   */
  getPartScoresByTeachingMissionAndStudent(teachingMissionId, studentId) {
    return request({
      url: `/api/scores/part/by-teaching-mission/${teachingMissionId}/student/${studentId}`,
      method: 'get'
    })
  },

  /**
   * 更新最终成绩
   * @param {Object} score 成绩信息，包含ID
   */
  updateFinalScore(score) {
    return request({
      url: '/api/scores/final',
      method: 'post',
      data: score
    })
  },

  /**
   * 创建或更新最终成绩
   * @param {Object} score 成绩信息
   */
  createOrUpdateFinalScore(score) {
    return request({
      url: '/api/scores/final',
      method: 'post',
      data: score
    })
  },

  /**
   * 删除最终成绩
   * @param {number} id 成绩ID
   */
  deleteFinalScore(id) {
    return request({
      url: `/api/scores/final/${id}`,
      method: 'delete'
    })
  },

  /**
   * 更新部分成绩
   * @param {Object} score 成绩信息，包含ID
   */
  updatePartScore(score) {
    return request({
      url: '/api/scores/part',
      method: 'post',
      data: score
    })
  },

  /**
   * 创建或更新部分成绩
   * @param {Object} score 成绩信息
   */
  createOrUpdatePartScore(score) {
    return request({
      url: '/api/scores/part',
      method: 'post',
      data: score
    })
  },

  /**
   * 删除部分成绩
   * @param {number} id 成绩ID
   */
  deletePartScore(id) {
    return request({
      url: `/api/scores/part/${id}`,
      method: 'delete'
    })
  },

  /**
   * 批量导入最终成绩
   * @param {FormData} formData 包含Excel文件和教学任务ID的表单数据
   */
  importFinalScores(formData) {
    return request({
      url: '/api/scores/final/import',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 批量导入部分成绩
   * @param {FormData} formData 包含Excel文件、教学任务ID和成绩组成ID的表单数据
   */
  importPartScores(formData) {
    return request({
      url: '/api/scores/part/import',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 计算指定教学任务的最终成绩
   * @param {number} teachingMissionId 教学任务ID
   */
  calculateFinalScores(teachingMissionId) {
    return request({
      url: `/api/scores/calculate/${teachingMissionId}`,
      method: 'post'
    })
  },

  /**
   * 获取教学任务的成绩统计数据
   * @param {number} teachingMissionId 教学任务ID
   */
  getScoreStatistics(teachingMissionId) {
    return request({
      url: `/api/scores/statistics/${teachingMissionId}`,
      method: 'get'
    })
  },

  /**
   * 获取多个教学任务的成绩统计数据
   * @param {Array} teachingMissionIds 教学任务ID数组
   */
  getMultipleScoreStatistics(teachingMissionIds) {
    return request({
      url: '/api/scores/statistics/multiple',
      method: 'post',
      data: { teachingMissionIds }
    })
  },

  /**
   * 获取教学任务的单项成绩统计数据
   * @param {number} teachingMissionId 教学任务ID
   */
  getExamPartStatistics(teachingMissionId) {
    return request({
      url: `/api/scores/exam-part-statistics/${teachingMissionId}`,
      method: 'get'
    })
  },

  /**
   * 获取教学任务基本信息
   * @param {number} teachingMissionId 教学任务ID
   */
  getMissionInfo(teachingMissionId) {
    return request({
      url: `/api/teaching-missions/${teachingMissionId}/info`,
      method: 'get'
    })
  }
}